package com.zfh.demo.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.zfh.demo.blog.entity.Student;
import com.zfh.demo.blog.mapper.StudentMapper;
import com.zfh.demo.blog.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周孚环
 * @since 2020-07-02
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public Page<Student> selectPage(Long current,long size,String name) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        Page<Student> page = (Page<Student>) studentMapper.selectPage(new Page<>(current, size), queryWrapper);
        return page;
    }

    @Override
    public List<Student> selectLike(String name) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Student> list = studentMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void addStudent(Student student) {
        studentMapper.insert(student);
    }


    @Override
    public List<Student> findAll(String name, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Student> list = studentMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Student> findStudentAll() {
        List<Student> students = studentMapper.selectList(null);
        return students;
    }

    @Override
    public Student perUpdateStudent(Integer id) {
        return this.studentMapper.selectById(id);
    }

    @Override
    @Transactional
    public void update(Student student) {
        studentMapper.updateById(student);
    }

    @Override
    public void daleta(Long id) {
        this.studentMapper.deleteById(id);
    }


}
